<?php
include "db_config.php";

class inancm {
	
	public function __construct(){
		$db = new DB_con();
	}
	
	public function subtext($text,$n){
		return iconv_substr($text,0,$n, "UTF-8")."...";
	}
	
	public function pre($values){
		echo "<pre>"; print_r($values); echo "</pre>";
	}
	
	public function checkArray($mid,$arr){
		$has_id = in_array($mid, $arr);	
		return $has_id;
	}
	
	public function dirTree($dir) {
		$objOpen = opendir($dir); 
		while (($file = readdir($objOpen)) !== false) 	{ 		
			if($file==".." || $file=="."){ 		
				//$fileshow = ""; 
			} else { 	
				if(!is_dir($file) && strpos($file, '.jpg')>0) { 	
					$fileshow[] = $file; 	
				}
			} 
		} 
		return $fileshow;
	}

	

	public function DateTimeDiff($strDateTime1,$strDateTime2){
		//$date = 0;
		//$hour = 0;
		//$min = 0;
		$diff = strtotime($strDateTime2) - strtotime($strDateTime1);
		$year = floor($diff / 31536000);
		$month = floor(($diff % 31536000) / 2592000);
		$date = floor(($diff-($year*31536000)-($month*2592000))/(86400));				
		return time_display($diff);						
	}
	
	

	public function time_display($seconds)
	{
		/*** return value ***/
		$ret = "";
	
		$Day = floor($seconds/86400);
		if($Day > 0)
		{
			$ret .= "$Day Day :";
		}
									
		/*** get the hours ***/
		$hours = floor(($seconds-($Day*86400))/60.0/60.0);
	
		if($hours > 0 || $Day > 0)
		{
			$ret .= "$hours : ";
		}
		/*** get the minutes ***/
		$minutes = floor(($seconds  - (($hours * 60.0 * 60.0) + ($Day*86400)) )/60.0);
		#$minutes = bcmod((intval($seconds) / 60),60);
		if($hours > 0 || $minutes > 0)
		{
			$ret .= "$minutes : ";
		}
									
		/*** get the seconds ***/
		$seconds = floor($seconds  - ( ($hours * 60.0 * 60.0) + ($Day*86400) + ($minutes*60) ) ) ;
		$ret .= "$seconds ";
		
		if ($Day <= 0) {
			
			if ($hours > 0) {
				return $hours." ชั่วโมง";
			} else {
						
				if ($minutes > 0) {
					return $minutes." นาที";
				} else {			
					if ($seconds > 0) { return $seconds." วินาที"; }
				}
			}
			
		} else {
			return $Day." วัน";
		}
		
	}
	
	
	public function Thai_date($day){
		$date = $day;
		$part_0 = explode(' ',$date);
		$part = explode('-',$part_0[0]);
		$thyear = $part[0];
		$month = $part[1];
		$thday = $part[2];
    	if($thday <10)
       		$thday = substr($thday,1,1);
       
    	$thMonth = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน",
                     "กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
    
    	/*$thMonth = array("ม.ค.","ก.พ.","มี.ค.","ม.ย.","พ.ค.","มิ.ย.",
                     "ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");*/
    
    	$thyear +=543;
		    
    	$day = $thday." ".$thMonth[$month-1]." ".$thyear;  
    	return $day;
	}
	
	
	
	public function count_data_mysql($id,$tbname,$select){
		$sql = "select count($id) as totaldata from $tbname where $select";		
		$rs = mysql_query($sql);
		$data = @mysql_fetch_object($rs) ;	
		$totaldata = $data->totaldata;		
		//echo $sql;
		return $totaldata;
	}
	
	
	public function currencyString($number){ 
		$txtnum1 = array('ศูนย์','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า','สิบ'); 
		$txtnum2 = array('','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน'); 
		$number = str_replace(",","",$number); 
		$number = str_replace(" ","",$number); 
		$number = str_replace("บาท","",$number); 
		$number = explode(".",$number); 
		
		if(sizeof($number)>2){ 
			return 'ทศนิยมหลายตัวนะจ๊ะ'; 
			exit; 
		} 
		
		$strlen = strlen($number[0]); 
		$convert = ''; 
		for($i=0;$i<$strlen;$i++){ 
			$n = substr($number[0], $i,1); 
			if($n!=0){ 
				if($i==($strlen-1) AND $n==1){ $convert .= 'เอ็ด'; } 
				elseif($i==($strlen-2) AND $n==2){  $convert .= 'ยี่'; } 
				elseif($i==($strlen-2) AND $n==1){ $convert .= ''; } 
				else{ $convert .= $txtnum1[$n]; } 
				$convert .= $txtnum2[$strlen-$i-1]; 
			} 
		} 
		
		$convert .= 'บาท'; 
		if($number[1]=='0' OR $number[1]=='00' OR 
			$number[1]==''){ 
			$convert .= 'ถ้วน'; 
		}else{ 
			$strlen = strlen($number[1]); 
			for($i=0;$i<$strlen;$i++){ 
				$n = substr($number[1], $i,1); 
				if($n!=0){ 
				if($i==($strlen-1) AND $n==1){$convert 
				.= 'เอ็ด';} 
				elseif($i==($strlen-2) AND 
				$n==2){$convert .= 'ยี่';} 
				elseif($i==($strlen-2) AND 
				$n==1){$convert .= '';} 
				else{ $convert .= $txtnum1[$n];} 
				$convert .= $txtnum2[$strlen-$i-1]; 
			} 
		} 
		$convert .= 'สตางค์'; 
		} 
		return $convert; 
	} 
	
	
	
	//Example of use:
    //connect();
	// ฟังก์ชันสำหรับเชื่อมต่อกับฐานข้อมูล  
    public function connect() {  
      // เริ่มต้นส่วนกำหนดการเชิ่อมต่อฐานข้อมูล //  
      $HOST="localhost"; // ชื่อ server หรือ domain name ปกติใช้ localhost  
      $PORT=""; // กำหนดหรือไม่ก็ได้  
      $DB_USER="root"; // ชื้อผู้ใช้  
      $DB_PWD="test"; // รหัสผ่าน  
      $DB_NAME="test";   // ชื่อฐานข้อมูล  
      // สิ้นสุุดส่วนกำหนดการเชิ่อมต่อฐานข้อมูล //   
      $DB_HOST=(!emptyempty($PORT)) ? $HOST.":".$PORT : $HOST;  
      if(@mysql_connect($DB_HOST,$DB_USER,$DB_PWD)){  
          $conServ=@mysql_select_db($DB_NAME) or die("SQL Error: <br>".mysql_error());        
      }else{  
          die("SQL Error: <br>".mysql_error());       
      }  
    }  
	
	//ฟังก์ชันสำหรับแสดงรายการฟิลด์ในตาราง
	public function listfield($table){
		$req=@mysql_query("SELECT * FROM $table");
		$numberfields =@mysql_num_fields($req);
		$row_title="\$data=array(<br/>";
		for($i=0; $i<$numberfields ; $i++ ) {
			   $var=@mysql_field_name($req, $i);
			   $row_title.="\"$var\"=>\"value$i\",<br/>";
		}
		$row_title.=");<br/>";
		echo $row_title;
	}
    
	
	/*
	วิธีใช้
	$Table_Name = 'tb_example';
	$data = array(
			'_firtname'=>$_POST['firstname'],
			'_lastname'=>$_POST['lastname'],
			'_nicname'=>$_POST['nicname']
	);
	$sql = insert_db($Table_Name, $data);
	mysql_query($sql);
	*/ 
    public function insert_db($nameTable, $data){
        while($element = each($data)){
            $insert[$element["key"]]="'".$element["value"]."'";
        }
        return $sql = "INSERT INTO `".$nameTable."` (".implode(",",array_keys($insert)).") VALUES (".implode(",",array_values($insert)).")";
    }

	/*
	วิธีใช้
	$Table_Name = 'tb_example';
	$data = array(
			'_firtname'=>$_POST['firstname'],
			'_lastname'=>$_POST['lastname'],
			'_nicname'=>$_POST['nicname']
	);
	$sql = update_db($tb_example, array('_id='=>$_POST['fid']), $data);
	mysql_query($sql);
	*/   
    public function update_db($nameTable, $where, $data){
        while($element = each($data)){
            $update[$element["key"]]="`".$element["key"]."`='".$element["value"]."'";
        }
        while($element2 = each($where)){
            $where[$element2["key"]]=$element2["key"]."'".$element2["value"]."'";
        }
		#echo $sql = "UPDATE `".$nameTable."` SET ".implode(",",$update)." WHERE ".implode(",",$where);
        return $sql = "UPDATE `".$nameTable."` SET ".implode(",",$update)." WHERE ".implode(",",$where);
    }

	/*
	วิธีใช้
	$Table_Name = 'tb_example';
	$sql = delete_db($tb_example, array('_id='=>$_GET['id']));
	mysql_query($sql);
	*/
    public function delete_db($nameTable, $where){
        while($element = each($where)){
            $where[$element["key"]]=$element["key"]."'".$element["value"]."'";
        }
        return $sql = "DELETE FROM ".$nameTable." WHERE ".implode(",",$where);
    }
	
	
	/*
	การใช้งาน
	$sql = "SELECT * FROM mytable";
	$results = select($sql);
	print_r($results);
	*/
	public function select_db($sql) {
		$result=array();	
		$req = mysql_query($sql) or die("SQL Error: ".$sql."".mysql_error());	
		while($data= mysql_fetch_assoc($req)) {	
			$result[] = $data;	
		}	
		return $result;	
	}
}
?>